package Client.Dropbox.Manager

import java.beans.XMLEncoder
import java.io.BufferedOutputStream
import java.io.FileOutputStream
import java.beans.XMLDecoder
import java.io.BufferedInputStream
import java.io.FileInputStream

/**
 * Object that loads and saves FileState instances using XML
 */
object FSLoader {
  
  /**
   * Save a FileState to an XML file
   * @param fs the FileState to save
   */
  def save (fs:FileState)= {
    try {
      val file=new FileOutputStream("filestate.xml")
      val encoder=new XMLEncoder (file)
      encoder.writeObject(fs)
      encoder.close
      file.close
    }
    catch {
      case e:Exception => System.err.println(e.getMessage)
    }
  }
  
  /**
   * Load a FileState from XML
   * @returns the FileState from the file
   */
  def load :FileState = {
    var decoder=null.asInstanceOf[XMLDecoder]
    try {
      decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("filestate.xml")))
    }
    catch {
      case e:Exception => {System.err.println(e.getMessage); return null}
    }
    return decoder.readObject.asInstanceOf[FileState] 
  }


}